<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>采购入库订单信息管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="/style/basic_layout.css" rel="stylesheet" type="text/css">
    <link href="/style/common_style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="/js/jquery/jquery.js"></script>
    <script src="/js/plugins/artDialog/jquery.artDialog.js?skin=blue"></script>
    <%--弹出层--%>
    <script src="/js/plugins/artDialog/plugins/iframeTools.source.js"></script>
    <script type="text/javascript" src="/js/plugins/jqueryForm/jquery.form.min.js"></script>
    <script type="text/javascript" src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="/js/commonAll.js"></script>


    <script>
        $(function () {
            //保存多条采购入库订单 给每行设置索引 
            $("#editForm").submit(function () {
                $.each($("#edit_table_body tr"), function (index, item) {
                    //    console.log(index);
                    //当前遍历行 设置提交商品id, 采购入库价格,采购入库数量 备注
                    $(item).find("[tag=pid]").prop("name", "items[" + index + "].product.id");
                    $(item).find("[tag=costPrice]").prop("name", "items[" + index + "].costPrice");
                    $(item).find("[tag=number]").prop("name", "items[" + index + "].number");
                    $(item).find("[tag=remark]").prop("name", "items[" + index + "].remark");
                   // console.log(item);
                })
            })


            $("#editForm").ajaxForm(function (data) {
                if (data.success) {
                    showDialog("保存成功", function () {
                        window.location.href = "/stockIncomeBill/list.do"
                    })
                } else {
                    showDialog(data.msg);
                }
            });
            //商品选择界面
            $(".searchproduct").click(function () {
                //1找到当前行 向上找
                var currentTr = $(this).closest("tr");
                var url = "/product/selectProduct.do";
                art.dialog.open(url, {
                    id: "abc",
                    title: "商品选择",
                    width: 900,
                    height: 700,
                    close: function () {
                        //取出数据操作
                        //  console.log($.dialog.data("abc"));
                        var productJSON = $.dialog.data("json");
                        //设置为空值
                        $.dialog.data("json", "");
                        //  console.log(productJSON);
                        //console.log($.dialog.data("json").bName);
                        if (productJSON) {//判断是否有值
                            //2向下找设置值 通过属性名称tag 找
                            currentTr.find("[tag=name]").val(productJSON.pName);
                            currentTr.find("[tag=pid]").val(productJSON.pid);
                            currentTr.find("[tag=brand]").html(productJSON.bName);
                            currentTr.find("[tag=costPrice]").val(productJSON.costPrice);
                        }
                    }
                })
            });
            //给价格数量绑定值改变事件
            $("[tag=costPrice],[tag=number]").change(function () {
                //   alert("111");
                //获取到当前行的价格和数量
                var currentTr = $(this).closest("tr");
                //找到价格和数量的值
                var costPirce = currentTr.find("[tag=costPrice]").val();
                var number = currentTr.find("[tag=number]").val();
                //过滤非法字符 第一个是false去NAN给默认值为0
                number = parseFloat(number) || 0;
                costPirce = parseFloat(costPirce) || 0;
                //    console.log(number);
                //     console.log(costPirce);
                var amount = (number * costPirce).toFixed(2);
                currentTr.find("[tag=amount]").html(amount);

            });

            //拷贝明细 操作
            $(".appendRow").click(function () {
                //1拷贝第一行内容
                var cloneTr = $("#edit_table_body tr:first").clone(true);
                //2清空拷贝的内容
                cloneTr.find(":input").val("");
                cloneTr.find("span").html("");
                //3添加到tbody中
                $("#edit_table_body").append(cloneTr);
            });
            //删除明细 操作
            $(".removeItem").click(function () {
//                alert(55);
                //找到当前行
                var currentTr = $(this).closest("tr");
                //判断是否有多行 有多行 删除当前行 否则清空改行数据
                if ($("#edit_table_body tr").size() > 1) {
                    currentTr.remove();
                } else {
                    currentTr.find(":input").val("");
                    currentTr.find("span").html("");
                }
            });

            //仓库回显
            $(":input[name='depot.id'] option[value='${stockIncomeBill.depot.id}']").prop("selected",true);

        })

    </script>
</head>
<body>
<form name="editForm" action="/stockIncomeBill/saveOrUpdate.do" method="post" id="editForm">
    <input type="hidden" name="id" value="${stockIncomeBill.id}">
    <%--审核状态隐藏域--%>
    <input type="hidden" name="status" value="${empty stockIncomeBill.status?0:stockIncomeBill.status}">

    <div id="container">
        <div id="nav_links">
            <span style="color: #1A5CC6;">采购入库订单编辑</span>
            <div id="page_close">
                <a>
                    <img src="/images/common/page_close.png" width="20" height="20" style="vertical-align: text-top;"/>
                </a>
            </div>
        </div>
        <div class="ui_content">
            <table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
                <tr>
                    <td class="ui_text_rt" width="140">采购入库订单编码</td>
                    <td class="ui_text_lt">
                        <input name="sn" value="${stockIncomeBill.sn}" class="ui_input_txt02"/>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">仓库</td>
                    <td class="ui_text_lt">
                        <select name="depot.id" class="ui_select03">
                            <c:forEach items="${depots}" var="item">
                                <option value="${item.id}">${item.name}</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">业务时间</td>
                    <td class="ui_text_lt">
                        <input name="vdate" class="ui_input_txt02 Wdate" onclick="WdatePicker()"
                        <%-- 回显时间--%>
                               value="<fmt:formatDate value="${stockIncomeBill.vdate}" pattern="yyyy-MM-dd"/>"/>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">单据明细</td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加明细" class="ui_input_btn01 appendRow"/>
                        <table class="edit_table" cellspacing="0" cellpadding="0" border="0" style="width: auto">
                            <thead>
                            <tr>
                                <th width="10"></th>
                                <th width="200">货品</th>
                                <th width="120">品牌</th>
                                <th width="80">价格</th>
                                <th width="80">数量</th>
                                <th width="80">金额小计</th>
                                <th width="150">备注</th>
                                <th width="60"></th>
                            </tr>
                            </thead>
                            <tbody id="edit_table_body">
                                <%--如果是新增就显示空白行,否则就显示原来得数据--%>
                            <c:choose>
                                <c:when test="${stockIncomeBill.id==null}">
                                    <tr>
                                        <td></td>
                                        <td>
                                            <input disabled="true" readonly="true" class="ui_input_txt02" tag="name"/>
                                            <img src="/images/common/search.png" class="searchproduct"/>
                                            <input type="hidden" name="items[0].product.id" tag="pid"/>
                                        </td>
                                        <td><span tag="brand"></span></td>
                                        <td><input tag="costPrice" name="items[0].costPrice" class="ui_input_txt00"/></td>
                                        <td><input tag="number" name="items[0].number" class="ui_input_txt00"/></td>
                                        <td><span tag="amount"></span></td>
                                        <td><input tag="remark" name="items[0].remark" class="ui_input_txt02"/></td>
                                        <td>
                                            <a href="javascript:;" class="removeItem">删除明细</a>
                                        </td>
                                    </tr>
                                </c:when>
                                <c:otherwise>
                                    <c:forEach items="${stockIncomeBill.items}" var="item">
                                        <tr>
                                            <td></td>
                                            <td>
                                                <input disabled="true" readonly="true" class="ui_input_txt02" tag="name" value="${item.product.name}"/>
                                                <img src="/images/common/search.png" class="searchproduct"/>
                                                <input type="hidden" name="items[0].product.id" tag="pid" value="${item.product.id}"/>
                                            </td>
                                            <td><span tag="brand">${item.product.brandName}</span></td>
                                            <td><input tag="costPrice" name="items[0].costPrice" class="ui_input_txt00" value="${item.costPrice}"/></td>
                                            <td><input tag="number" name="items[0].number" class="ui_input_txt00" value="${item.number}"/></td>
                                            <td><span tag="amount">${item.amount}</span></td>
                                            <td><input tag="remark" name="items[0].remark" class="ui_input_txt02" ${item.remark}/></td>
                                            <td>
                                                <a href="javascript:;" class="removeItem">删除明细</a>
                                            </td>
                                        </tr>

                                    </c:forEach>
                                </c:otherwise>
                            </c:choose>
                            </tbody>
                        </table>
                    </td>
                </tr>

                <tr>
                    <td>&nbsp;</td>
                    <td class="ui_text_lt">
                        &nbsp;<input type="submit" value="确定保存" class="ui_input_btn01"/>
                        &nbsp;<input id="cancelbutton" type="button" value="重置" class="ui_input_btn01"/>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
</body>
</html>